<template>
  <section class="real-app">
    <input type="text" class="add-input" autofocus="autofocus" placeholder="接下去要做什么？" @keyup.enter="addTodo">
    <item :todo="todo" v-for="todo in filteredTodos" :key="todo.id" @del="deleteTodo" />
    <tabs :filter="filter" :todos="todos" @toggle="toggleFilter" @clearAllCompleted="clearAllCompleted"
    />
  </section>
</template>

<script>
import Item from './item.vue';
import Tabs from './tabs.vue';
let id = 0;
export default {
  metaInfo: {
    title: 'this is todo',
  },
  data() {
    return {
      todos: [],
      filter: 'all',
    };
  },
  // props:['id'],
  // props: ['name'],
  // props: ['query'],
  components: {
    Item,
    Tabs,
  },
  computed: {
    filteredTodos() {
      if (this.filter === 'all') {
        return this.todos;
      }
      const completed = this.filter === 'completed';
      return this.todos.filter(todo => completed === todo.completed);
    },
  },
  methods: {
    addTodo(e) {
      this.todos.unshift({
        id: id++,
        content: e.target.value.trim(),
        completed: false,
      });
      e.target.value = '';
    },
    deleteTodo(id) {
      this.todos.splice(this.todos.findIndex(todo => todo.id === id), 1);
    },
    toggleFilter(state) {
      this.filter = state;
    },
    clearAllCompleted() {
      this.todos = this.todos.filter(todo => !todo.completed);
    },
  },
  mounted() {
    // console.info('props的参数', this.id);
    // console.info('props的参数', this.name);
    // console.info('props的参数', this.query);
  },
  beforeRouteEnter(to, from, next) {
    // 拿到数据 赋值给变量中，拿不到this
    next(vm => {
      console.info('beforeRouteEnter', this, 'vm', vm);
    });
  },
  beforeRouteUpdate(to, from, next) {
    // 一般params的时候 监听变化操作
    // 或者 要watch id的变化，性能浪费
    console.info('beforeRouteUpdate', this);
    next();
  },
  beforeRouteLeave(to, from, next) {
    // 离开页面  需要销毁
    // 表单修改 是否离开当前页面
    console.info('beforeRouteLeave', this);
    if (global.confirm('确认离开吗')) {
      next();
    }
    next();
  },
};
</script>

<style lang="stylus" scoped>
.real-app {
  width: 600px;
  margin: 0 auto;
  box-shadow: 0 0 5px #666;
}

.add-input {
  position: relative;
  margin: 0;
  width: 100%;
  font-size: 24px;
  font-family: inherit;
  font-weight: inherit;
  line-height: 1.4em;
  border: 0;
  outline: none;
  color: inherit;
  padding: 6px;
  border: 1px solid #999;
  box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
  font-smoothing: antialiased;
  padding: 16px 16px 16px 60px;
  border: none;
  box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);
}
</style>

